package com.ztc;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

import java.io.*;
import java.net.Socket;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by ztc on 16-1-17.下午2:20
 */
public class spider {
    //飞鱼测试账户ｕｒｌ
    String url="http://ss.feiyuvpn.cn/page/testss.html";
    CloseableHttpClient client;
    public static void main(String[] args){
        if(args.length==0) {
            spider s = new spider();
            s.writeJson(s.getContent(), "1080","10");
            s.rmRedun();
            System.out.println("用法:\n" +
                    "java -jar FreeSS [port][length]\n" +
                    "port:本地链接端口,默认为１０８０\n" +
                    "length:保存的个数,默认１０\n");
        }else if(args.length==1){
            spider s = new spider();
            s.rmRedun();
            s.writeJson(s.getContent(), args[0],"10");
        }else if(args.length==2){
            spider s = new spider();
            s.rmRedun();
            s.writeJson(s.getContent(), args[0],args[1]);
        }else {
            System.out.println("用法:\n" +
                    "java -jar FreeSS [port]\n" +
                    "port:本地链接端口,默认为１０８０\n"+
                    "length:保存的个数，默认１０\n");
        }
    }
    public String getContent(){
        client= HttpClients.createDefault();
        HttpGet get=new HttpGet(url);
        StringBuilder sb=new StringBuilder();
        try {
            CloseableHttpResponse response=client.execute(get);
            HttpEntity entity=response.getEntity();
            InputStream in=entity.getContent();
            byte[] buffer=new byte[1024];
            int len;
            while((len=in.read(buffer))!=-1){
                String bu=new String(buffer,0,len);
                sb.append(bu);
            }
            in.close();
            response.close();
            client.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return sb.toString();
    }
    public void writeJson(String content,String port,String length){
        //System.out.println(content);
        String reg0="测试节点：(.*?)</b>",JD1=null,JD2=null;
        String reg1="服务器IP：<span>(.*?)</span>",IP1=null,IP2=null;
        String reg2="端口：(.*?)<br />",PORT1=null,PORT2=null;
        String reg3="密码：(.*?)<br />",PW1=null,PW2=null;
        String reg4="加密方式：<span>(.*?)</span>",SE1=null,SE2=null;
        Pattern pattern0=Pattern.compile(reg0);
        Pattern pattern1=Pattern.compile(reg1);
        Pattern pattern2=Pattern.compile(reg2);
        Pattern pattern3=Pattern.compile(reg3);
        Pattern pattern4=Pattern.compile(reg4);
        Matcher matcher0=pattern0.matcher(content);
        Matcher matcher1=pattern1.matcher(content);
        Matcher matcher2=pattern2.matcher(content);
        Matcher matcher3=pattern3.matcher(content);
        Matcher matcher4=pattern4.matcher(content);
        while(matcher0.find()&&matcher1.find()&&matcher2.find()&&matcher3.find()&&matcher4.find()){
            System.out.println(matcher0.group(1));
            System.out.println(matcher1.group(1));
            System.out.println(matcher2.group(1));
            System.out.println(matcher3.group(1));
            System.out.println(matcher4.group(1));
            if(JD1==null) {
                JD1 = matcher0.group(1);
                IP1 = matcher1.group(1);
                PORT1 = matcher2.group(1);
                PW1 = matcher3.group(1);
                SE1 = matcher4.group(1);
            }else{
                JD2 = matcher0.group(1);
                IP2 = matcher1.group(1);
                PORT2 = matcher2.group(1);
                PW2 = matcher3.group(1);
                SE2 = matcher4.group(1);
            }
        }
        String sb1="{ \n" +
                "\"server\":\""+IP1+"\", \n" +
                "\"server_port\":"+PORT1+", \n" +
                "\"local_address\":\"127.0.0.1\", \n" +
                "\"local_port\":"+port+", \n" +
                "\"password\":\""+PW1+"\", \n" +
                "\"timeout\":300, \n" +
                "\"method\":\""+SE1+"\", \n" +
                "\"fast_open\": false\n" +
                "}";
        String sb2="{ \n" +
                "\"server\":\""+IP2+"\", \n" +
                "\"server_port\":"+PORT2+", \n" +
                "\"local_address\":\"127.0.0.1\", \n" +
                "\"local_port\":"+port+", \n" +
                "\"password\":\""+PW2+"\", \n" +
                "\"timeout\":300, \n" +
                "\"method\":\""+SE2+"\", \n" +
                "\"fast_open\": false\n" +
                "}";
        File SS=new File("SS");
        if(!SS.isDirectory()){
            boolean result=SS.mkdir();
            System.out.println("mkdir:"+result);
        }
        File[] ss=SS.listFiles();
        try {
            assert ss != null;
            if(JD1!=null&&ss.length<Integer.parseInt(length)) {
                FileWriter fw1 = new FileWriter("SS/" + JD1 + ".json");
                fw1.write(sb1);
                fw1.close();
            }
            if(JD2!=null&&ss.length<Integer.parseInt(length)){
                FileWriter fw2 = new FileWriter("SS/" + JD2 + ".json");
                fw2.write(sb2);
                fw2.close();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    public void rmRedun(){
        File SS=new File("SS");
        if(!SS.isDirectory()){
            return;
        }
        File[] files=SS.listFiles();
        assert files != null;
        for(File file:files){
            if(!isValidity(file)){
                System.out.println("删除无效JSON:"+file.getName()+"　结果："+file.delete());
            }
        }
    }
    public boolean isValidity(File file){
        try {
            FileInputStream in=new FileInputStream(file);
            String ip=null,port=null,content="";
            byte[] buffer=new byte[1024];
            int len;
            while((len=in.read(buffer))!=-1){
                String bu=new String(buffer,0,len);
                content+=bu;
            }
            String reg="\"server\":\"(.*?)\", \n" +
                    "\"server_port\":(.*?), ";
            Pattern pattern=Pattern.compile(reg);
            Matcher matcher= pattern.matcher(content);
            if(matcher.find()) {
                ip = matcher.group(1);
                port = matcher.group(2);
                System.out.println("测试：" + ip + ":" + port);
            }
            if(ip!=null&&!ip.equals("")&&!port.equals("null")&&!port.equals("")) {
                Socket s = new Socket(ip, Integer.parseInt(port));
                boolean result=s.isConnected();
                s.close();
                return result;
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }
}
